package com.example.administrator.zkdemo.core.interfaces;


import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

/**
 * @author 丁可
 * @package com.example.administrator.zkdemo.core.interfaces
 * @fileName DatabaseReceiver
 * @date 2018/11/19 0019 10:29
 * @description
 * @org http://www.gshbzw.com/ 人和致远数据服务有限责任公司
 */
public abstract class DatabaseReceiver extends BroadcastReceiver {
    public static String ACTION_ON_ADD_PEOPLE_BIOMETRIC = "com.zkteco.android.core.add.people.biometric";
    public static String ACTION_ON_ADD_PEOPLE_PHOTO = "com.zkteco.android.core.add.people.photo";
    public static String ACTION_ON_DELETE_ALL_PEOPLE_BIOMETRIC = "com.zkteco.android.core.delete.all.people.biometric";
    public static String ACTION_ON_DELETE_PEOPLE_BIOMETRIC = "com.zkteco.android.core.delete.people.biometric";
    public static String ACTION_ON_TABLE_MODIFIED = "com.zkteco.android.core.db.TABLE_MODIFIED";
    public static String DB_ID = "id";
    public static String TABLE_NAME = "table-name";
    private static final String TAG = "DatabaseReceiver";
    private String databaseId;
    private Set<String> tableFilter;

    public void onReceive(Context context, Intent paramIntent) {
        String paramContext = paramIntent.getStringExtra(DB_ID);
        Object localObject = TAG;
        StringBuilder localStringBuilder = new StringBuilder();
        localStringBuilder.append("Our DB ID: ");
        localStringBuilder.append(this.databaseId);
        localStringBuilder.append("; received db id: ");
        localStringBuilder.append(paramContext);
        Log.d((String) localObject, localStringBuilder.toString());
        if (paramContext == null) {
            localObject = TAG;
            localStringBuilder = new StringBuilder();
            localStringBuilder.append("Received DB intent without DB id: ");
            localStringBuilder.append(paramIntent);
            Log.w((String) localObject, localStringBuilder.toString());
        }
        if (!this.databaseId.equals(paramContext)) {
            return;
        }
        if (paramIntent.getAction().equals(ACTION_ON_TABLE_MODIFIED)) {
            Log.d(TAG, "Received table changed broadcast");
            HashSet paramContext1 = (HashSet) paramIntent.getSerializableExtra(TABLE_NAME);
            Iterator paramIntent1 = this.tableFilter.iterator();
            while (paramIntent1.hasNext()) {
                localObject = (String) paramIntent1.next();
                if (paramContext.contains((CharSequence) localObject)) {
                    onTableModified((String) localObject);
                }
            }
        }
        paramContext = TAG;
        localObject = new StringBuilder();
        ((StringBuilder) localObject).append("Received unknown action ");
        ((StringBuilder) localObject).append(paramIntent.getAction());
        Log.w(paramContext, ((StringBuilder) localObject).toString());
    }

    protected abstract void onTableModified(String paramString);

    public void register(Context paramContext, String paramString) {
        this.databaseId = paramString;
        if (this.tableFilter != null) {
            paramContext.registerReceiver(this, new IntentFilter(ACTION_ON_TABLE_MODIFIED));
            paramContext.registerReceiver(this, new IntentFilter(ACTION_ON_ADD_PEOPLE_BIOMETRIC));
            paramContext.registerReceiver(this, new IntentFilter(ACTION_ON_DELETE_PEOPLE_BIOMETRIC));
            paramContext.registerReceiver(this, new IntentFilter(ACTION_ON_DELETE_ALL_PEOPLE_BIOMETRIC));
            paramContext.registerReceiver(this, new IntentFilter(ACTION_ON_ADD_PEOPLE_PHOTO));
        }
    }

    public void setTableFilter(Set<String> paramSet) {
        this.tableFilter = paramSet;
    }

    public void unregister(Context paramContext) {
        paramContext.unregisterReceiver(this);
    }
}
